﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MHPL_DoTrangSuc.Data;
using MHPL_DoTrangSuc.Bussiness;

namespace MHPL_DoTrangSuc.ManHinh
{
    public partial class frmThanhToan_HoaDon : Form
    {
        KetNoi kn = new KetNoi();
        public static string mahoadon;
        
        public frmThanhToan_HoaDon()
        {
            InitializeComponent();
        }

        private void frmThanhToan_HoaDon_Load(object sender, EventArgs e)
        {
            kn.ketnoi();
            txtMaHoaDon.Text = "";
            txtMaDonHang.Text = "";
            hienthi_lstHoaDon();
          
        }
        public void hienthi_lstHoaDon()
        {
            string str = "SELECT MaHoaDon,NgayLapHoaDon,SoTienThanhToan,MaDonHang,TenQT FROM HoaDon hd, QuanTri qt  where hd.TenDNQT=qt.TenDNQT  ";
            DataTable tb = new DataTable();
            tb.Load(kn.ThucHienReader(str));
            Load_ListView(listViewHD, tb);
        }
               
        public void Load_ListView(ListView lvwName, DataTable tb)
        {
            listViewHD.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string[] col = new string[tb.Columns.Count];
                for (int j = 0; j < tb.Columns.Count; j++)
                {
                    col[j] = tb.Rows[i][j].ToString();
                }
                ListViewItem lvItem = new ListViewItem(col);
                lvwName.Items.Add(lvItem);
            }
        }

       

        private void listViewHD_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listViewHD.FocusedItem.Index;
            if (i < 0)
                return;
            txtMaHoaDon.Text = listViewHD.Items[i].SubItems[0].Text;
            txtMaDonHang.Text = listViewHD.Items[i].SubItems[3].Text;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            kn.Luu("UPDATE DonHang SET TrangThai=N'True' WHERE MaDonHang='" + txtMaDonHang.Text + "'");
            kn.Luu("DELETE FROM HoaDon WHERE MaHoaDon='" + txtMaHoaDon.Text + "'");
            
            MessageBox.Show("Hóa đơn đã được thanh toán");
            frmThanhToan_HoaDon_Load(sender, e);
        }
        
            

            
            
        
    }
}
